package com.enjoy.xbase.tools;

import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EditTextTools {

    /**
     * 设置输入框不能输入空格 特殊字符等..
     */
    public static void setEditTextSZ(EditText... editTexts) {
        for (EditText editText : editTexts) {
            setEditTextSZ(editText);
        }
    }

    /**
     * 设置输入框不能输入空格 特殊字符等..
     */
    public static void setEditTextSZ(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Pattern p = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                        Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
                String speChat = "[-_`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、{}？]";
                Pattern pattern = Pattern.compile(speChat);

                Matcher matcher = pattern.matcher(source.toString());
                Matcher m = p.matcher(source);

                if (matcher.find()) return "";
                if (source.equals(" ")) return "";
                if (m.find()) return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }
}
